package cn.com.niit.controller;
import cn.com.niit.pojo.Merchant;
import cn.com.niit.pojo.User;
import cn.com.niit.service.MerchantService;
import cn.com.niit.service.SessionService;
import cn.com.niit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
@CrossOrigin(origins = "*")
public class MainController {
    @Autowired
    UserService userService;
    @Autowired
    SessionService sessionService;
    @Autowired
    MerchantService merchantService;

    @GetMapping("/")
    public String index(HttpSession session, String name) {
        sessionService.delNull();
        if (sessionService.queryCount(name).isEmpty()) {
            session.setAttribute("USER", userService.queryCount(name));
            sessionService.insert(name);
            sessionService.delNull();
            return "index";
        } else return "c";
    }

    @GetMapping("/submit.html")
    public String submit() {
        return "submit";
    }

    @GetMapping("/login.html")
    public String login() {
        return "login";
    }

    @GetMapping("/shopSubmit")
    public String shopSubmit() {
        return "shopSubmit";
    }

    @GetMapping("/admin")
    public String admin(HttpSession session, String name) {
        sessionService.delNull();
        if (sessionService.queryCount(name).isEmpty()) {
            session.setAttribute("admin", userService.queryCount(name));
            sessionService.insert(name);
            sessionService.delNull();
            return "admin";
        } else return "c";
    }

    @GetMapping("/admin-user")
    public String admin_user() {
        return "admin-user";
    }

    @GetMapping("/admin-merchant-apply")
    public String admin_merchant_apply() {
        return "admin-merchant-apply";
    }

    @GetMapping("/shop")
    public String shop(HttpSession session, String name) {
        sessionService.delNull();
        if (sessionService.queryCount(name).isEmpty()) {
            sessionService.insert(name);
            session.setAttribute("SHOP", merchantService.showOne(name));
            session.setAttribute("USER", userService.queryCount(name));
            sessionService.delNull();
            return "shop";
        } else {
            return "c";
        }
    }

    @GetMapping("/logout")
    public String out(HttpSession session, String name,String kind) {
        if (kind.equals("USER")){ session.removeAttribute("USER");}
        if (kind.equals("SHOP")){ session.removeAttribute("SHOP");}
        if (kind.equals("admin")){ sessionService.del("admin");;}
        sessionService.del(name);
        sessionService.delNull();
        return "index";
    }

    @GetMapping("/user")
    public String user(HttpSession session, String name) {
        sessionService.delNull();
        if (sessionService.queryCount(name).isEmpty()) {
            sessionService.insert(name);
            session.setAttribute("USER", userService.queryCount(name));
            sessionService.delNull();
            return "user";
        } else return "c";
    }

    @GetMapping("/a.html")
    public String a() {
        return "a";
    }

    @GetMapping("/b.html")
    public String b() {
        return "b";
    }

    @GetMapping("/baned")
    public String baned() {
        return "baned";
    }

    @GetMapping("/back")
    public String back() {
        return "index";
    }

    @GetMapping("/toUser")
    public String toUser1(String userName, Model model) {
        Merchant merchant = merchantService.showOne(userName);
        model.addAttribute("shop", merchant);
        return "user";
    }
    @GetMapping("/toUser2")
    public String toUser2(String shopName,HttpSession session,String userName, Model model) {
        session.setAttribute("USER", userService.queryCount(userName));
        Merchant merchant = merchantService.showOne(shopName);
        model.addAttribute("shop", merchant);
        return "user";
    }
    @GetMapping("/reload")
    public String reload(String name,HttpSession session){
        session.setAttribute("USER", userService.queryCount(name));
        return "index";
    }
    @GetMapping("/shop1")
    public String shop1(HttpSession session, String name) {
        sessionService.delNull();
        sessionService.insert(name);
        session.setAttribute("SHOP", merchantService.showOne(name));
        session.setAttribute("USER", userService.queryCount(name));
        sessionService.delNull();
        return "shop";
    }
}
